//: src/com/silvtar/util/SSProfileMD.java
/* 
 * Created by 朴晟宁 <piaosn@gmail.com> on 5/26/2013. 
 * Copyright (c) 2013 朴晟宁. All rights reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program;
 * 
 * *********************************************************************
 * 
 * Modify History:
 * Date        Version    Description
 * 5/26/2013   0.1        Create the class.
 */

package com.silvtar.util;

import com.silvtar.io.SSFile;

/**
 * 用来读取md格式的配置文件的类
 * 
 * @author 朴晟宁/Silvtar
 * @author piaosn@gmail.com
 * @version 0.2
 */
public class SSProfileMD {

    public static class PfItem {
        String key;
        String parameter;

        final public String getKey() {
            return key;
        }

        final public String getParameter() {
            return parameter;
        }
    }

    String filePath;
    SSFile ssFile;
    long   position;

    /**
     * 
     * @param filePath
     *            配置文件路径
     */
    public SSProfileMD(String filePath) {

        this.filePath = filePath;
        ssFile = new SSFile(filePath, "rw");
    }

    /**
     * 删除给定字符串中的空格，回车，制表符，换行符
     * 
     * @param source
     *            源字符串
     * @return 处理好的字符串
     */
    public static String removeSpace(String source) {
        return source.replace(" ", "").replace("\n", "").replace("\t", "")
                .replace("\r", "");
    }

    /**
     * 获得下一项
     * 
     * @return
     */
    public PfItem getNextItem() {

        PfItem pfItem = null;
        String line = null;

        position = ssFile.getFilePointer();
        for (position = ssFile.getFilePointer(); (line = ssFile.readLine()) != null; position = ssFile
                .getFilePointer()) {
            if (line.equals("")) {
                continue;
            }
            if (Character.isLetter(line.charAt(0))) {
                pfItem = new PfItem();
                pfItem.key = removeSpace(line.substring(0, line.indexOf("=")));
                pfItem.parameter = removeSpace(line
                        .substring(line.indexOf("=") + 1));
                break;
            }
        }

        return pfItem;
    }

    /**
     * 搜索下一个指定项
     * 
     * @param key
     *            要搜索的键
     * @return 对应键的值
     */
    public String searchNext(String key) {

        PfItem pfItem = null;
        String parameter = null;

        while ((pfItem = getNextItem()) != null) {
            if (key.equals(pfItem.getKey())) {
                parameter = pfItem.getParameter();
            }
        }
        return parameter;
    }

    public void editNextParameter(String parameter) {

    }

    public void appendItem(String key, String parameter) {
        ssFile.seekLast();
        ssFile.writeUTF("\n" + key + " = " + parameter + "\n");
    }

    /**
     * 关闭配置文件
     */
    public void close() {
        ssFile.close();
    }

}
